<?php  
class ControllerCommonBirthday extends Controller {
	
	public function index() {
		$this->document->setTitle($this->config->get('config_title'));
		$this->document->setDescription($this->config->get('config_meta_description'));
		
		$this->data['heading_title'] = $this->config->get('config_title');
		
		$this->load->model('catalog/information');
		$information_info = $this->model_catalog_information->getInformation(12);
		
		$this->data['title'] = $information_info['title']; 		
			
		$this->data['description'] = html_entity_decode($information_info['short'], ENT_QUOTES, 'UTF-8');
		//$data['info_birthday'] = 'Thông tin đăng kí nhân ngày sinh nhật của bé thành công';
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			//print_r($_FILES);
			
			if (!empty($this->request->files['babyphoto']['name'])) {
				//$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
				$filename = basename($this->request->files['babyphoto']['name']);
				
				if ((strlen($filename) < 3) || (strlen($filename) > 128)) {
					//$json['error'] = $this->language->get('error_filename');
					$this->data['info_birthday'] = 'Lỗi ảnh của bé';
				}	  	
				
				$allowed = array();
				
				$filetypes = explode(',','jpg,JPG,jpeg,png');
				
				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}
				
				if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
					//$json['error'] = $this->language->get('error_filetype');
					$this->data['info_birthday'] = 'Lỗi ảnh của bé';
				}	
							
				if ($this->request->files['babyphoto']['error'] != UPLOAD_ERR_OK) {
					//$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
					$this->data['info_birthday'] = $this->request->files['babyphoto']['error'];
				}
			} else {
				$this->data['info_birthday'] = 'Lỗi ảnh của bé';
			}
			//print_r($data['info_birthday']);
			//print_r($this->request->files);
			if($this->request->files['babyphoto']['size'] > 1048576) {
				$this->data['info_birthday'] = 'Lỗi kích thước ảnh của bé upload phải nhỏ hơn 2M';
			} 
			if(!isset($this->data['info_birthday'])) 
			{
				if (is_uploaded_file($this->request->files['babyphoto']['tmp_name'])) {
				$ext = substr(strrchr($this->request->files['babyphoto']['name'], '.'), 1);
				
				$filename = date("dmyHis").'.'.$ext;
				move_uploaded_file($this->request->files['babyphoto']['tmp_name'], DIR_IMAGE .'data/birthday/'. $filename);
				}
				$this->load->model('catalog/information');
				$bday = explode('/',$this->request->post['birthday']);
				$birday = $bday[2].'-'.$bday[1].'-'.$bday[0];
				$this->data['bday'] = array(
				'module'     => 'birthday',
        		'status'     => 0,
				'nick_chat'  => $this->request->post['nick_chat'],
				'image'     =>  'data/birthday/'.$filename,
				'link'     => $this->request->post['phone'],
				'namechild'     => $this->request->post['namechild'],
				'sex'     => $this->request->post['sex'],
				'birthday'     => $birday,
				'information_description' => array(
					'2'=>array(
							'title' =>  $this->request->post['title'],
							'short' =>  '',
							'description' =>  $this->request->post['description'],
						)
				),
				'keyword'=>''
        		);
				//print_r($this->data['bday']);
			//exit();
				if($this->model_catalog_information->addBirthday($this->data['bday'])) {
					//if ($this->request->files['image']['size'] > 300000) {
					//}
					
					//print_r($this->request);
					//print_r($this->request->post);
					$this->data['info_birthday'] = 'Đăng kí thông tin thành công';
					
				} else {
					$this->data['info_birthday'] = 'Dăng kí thông tin không thành công';	
				}
				
			}
			//$data['info_birthday'] = 'Thông tin đăng kí nhân ngày sinh nhật của bé đăng kí không thành công';		
				
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/birthday.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/birthday.tpl';
		} else {
			$this->template = 'default/template/common/birthday.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'module/mebuu_latest',
			'module/mebuu_left_info_shop',
			'module/mebuu_about_home',
			'module/mebuu_product_typical',
			'module/mebuu_right_info_home',
			'module/mebuu_right_info_transport_home',
			'module/mebuu_right_info_birthday',
			'module/mebuu_category_about',
			'module/mebuu_category_shop',
			'module/mebuu_center_manufacturer_shop',
			'module/mebuu_consulting_home',
			'module/mebuu_faq_home',
			'module/mebuu_supportonline_home',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
										
		$this->response->setOutput($this->render());
	}
}
?>